package com.itat.cases;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Q：Java函数测试
 * @author Darren bo
 * @version 1.0
 * @date 2021/5/11
 */
public class JavaFunctionCase {


    /**
     * Continue跳过测试
     *  测试结果: break可以在for循环，或者多分枝选择（switch）里面使用;continue都只能在for循环使用
     *  break会直接跳出当前循环
     *  continue会跳过执行到continue后的逻辑，而与是否有if无关
     */
    public static void checkContinue(){

        String breakName="1";
        String doBreakJ1="1";

        for (int j = 0; j < 4; j++) {
            System.out.println("开始j输出："+j);
            if(!"".equals(breakName)){
                boolean isExistsj1=false;
                for (int i = 0; i < 4; i++) {
                    if((""+j).equals(doBreakJ1)){
                        isExistsj1=true;
                        break;//跳出验证
                    }
                }
                if (isExistsj1) {//只要为true，会跳过所在for循环continue字段后面的逻辑，而与if无关
                    continue;
                }
                System.out.println("不跳过（if内）业务j=："+j);//执行continue会跳过
            }
            System.out.println("不跳过（if外）业务j=："+j);//执行continue会跳过

        }

    }


    /**
     * Break 会跳出判断，不会继续执行case，如果没有break，则会把所有的case执行一遍
     */
    public static void checkBreak(){
        int j=1;
        switch(j){
            case 1: System.out.println("switch 1");break;
            case 2:System.out.println("switch 2");break;
            case 3:System.out.println("switch 3");break;
        }
    }


    /**
     * jdk8测试
     */
    public static void jdk8list(){
        Map<Integer, List<String>> syncThreeStatusHisMap = new HashMap<>();
        List<String> listTarget =  new ArrayList(Arrays.asList("java", "python", "C++","php"));
        List<String> listTarget1 = new ArrayList(Arrays.asList("java1", "python1", "C++1","php1"));
        syncThreeStatusHisMap.put(1,listTarget);

        //数据收集之list追加
        if(syncThreeStatusHisMap!=null && syncThreeStatusHisMap.containsKey(1)) { //如果存在对应的key，则追加list
            List<String> tmpList = syncThreeStatusHisMap.get(1).stream().sequential().collect(Collectors.toCollection(() -> listTarget1));
            syncThreeStatusHisMap.put(1,tmpList);
            for (int i = 0; i < tmpList.size(); i++) {
                System.out.println("temp value："+tmpList.get(i));
            }
        }else{
            syncThreeStatusHisMap.put(1,listTarget);
            for (int i = 0; i < listTarget.size(); i++) {
                System.out.println("value："+listTarget.get(i));
            }
        }



    }


    /**
     * Q：价格失真测试,精度丢失
     */
    public static  void priceQTest(){
        String strNum="0.01";
        Double dNum=0.01d;
        //数字
        BigDecimal price1 = new BigDecimal(dNum);
        System.out.println("price1："+price1);
        System.out.println("dprice1："+price1.doubleValue());
        //字符串
        BigDecimal price2 = new BigDecimal(strNum);
        System.out.println("price2："+price2);
        System.out.println("dprice2："+price2.doubleValue());
        //数字
        BigDecimal price3 = price1.setScale(2,BigDecimal.ROUND_HALF_UP);
        System.out.println("price1->price3："+price3);
        Double dprice3=price3.doubleValue();
        System.out.println("dprice3："+dprice3);
        System.out.println("fprice3："+price3.floatValue());
        //数字
        BigDecimal skuPrice =price1.setScale(2,BigDecimal.ROUND_HALF_UP);
        BigDecimal price = new BigDecimal(skuPrice.toString());
        System.out.println("dprice："+price.doubleValue());

    }

    /**
     * Q: Integer类型判断相等
     *  可以用equals判断，使用==判断会出问题
     */
    public static  void equalsInteger(){
        Integer num1=123;
        Integer num2=123;
        if (!num1.equals(num2)){
            System.out.println("equals: num1!=num2："+num1+","+num2+"");
        }else{
            System.out.println("equals: num1==num2："+num1+","+num2+"");
        }
        Integer num3=123456;
        Integer num4=123456;
        if (!num3.equals(num4)){
            System.out.println("equals: num3!=num4："+num3+","+num4+"");
        }else{
            System.out.println("equals: num3==num4："+num3+","+num4+"");
        }

        if (num3 == num4){
            System.out.println("== :num3==num4："+num3+","+num4+"");
        }else{
            System.out.println("== :num3!=num4："+num3+","+num4+"");
        }

        if (num3>=num4){
            System.out.println("num3>=num4："+num3+","+num4+"");
        }

        Integer num5=123458;
        if (num3>=num5){
            System.out.println("num3>=num5："+num3+","+num5+"");
        }


    }


    /**
     * Q:值传递问题，
     * 值传递不会改变值，引用传递会改变值
     */
    public static void valuePassed_a(){
        //值传递
        Boolean a=true;
        Integer b=2;
        String  c="3";
        //引用传递,对象
        StringBuilder d=new StringBuilder("4");//引用传递,对象
        final Boolean[] e = {false};
        System.out.println(String.format("before>> a=：%s，b=：%s，c=：%s，d=：%s，e=：%s",a,b,c,d.toString(),e[0]));
        //开始赋值
        valuePassed_B(a,b,c,d,e);
        System.out.println(String.format("after>> a=：%s，b=：%s，c=：%s，d=：%s，e=：%s",a,b,c,d.toString(),e[0]));
    }
    private static void valuePassed_B(Boolean a,Integer b,String c,StringBuilder d, Boolean[] e){
         a=false;
         b=222;
         c="333";
        d.append("c");
        e[0] =true;
    }


}
